package TAIC.text;
import java.io.*;
import java.util.*;

public class LanguageVec {
	HashMap < String , Integer > vec = null ;
	String filename ; 
	
	public static void main ( String args[]){
		if ( args.length < 1 ) {
			System.out.println ("please input the vec file to varify") ;
			return ;
		}
		( new LanguageVec( args[ 0 ]) ).init () ;
	}
	
	
	public LanguageVec ( String filename ) {
		this.filename = filename ; 
	}
	
	
	public void init () {
		if ( vec != null ) return ; 
		try {
			vec = new HashMap < String , Integer > () ;
			File file = new File ( filename ) ;
			if ( file.exists() ) {
				//int test = 0 ; 
				Scanner scanner = new Scanner ( new FileInputStream ( file )) ;
				scanner.nextInt() ;
				while ( scanner.hasNext()) {
					String word = scanner.next() ;
					int id = scanner.nextInt();
					vec.put( word ,  id ) ;
					//if ( test == vec.size() ) System.out.println ( word ) ;
				//	test ++ ; 
				}
				//System.out.println ( test + " " + vec.size() ) ;
				scanner.close(); 
			}
		}catch ( Exception e ){
			e.printStackTrace() ; 
		}
	}
	
	public int getID ( String str ) {
		if ( vec == null ) init () ;
		str= str.toLowerCase(); 
		Integer result = vec.get( str ) ;
		if ( result == null ) {
			result = vec.size() ;
			vec.put( str , vec.size() ) ;
		}
		return result ;  
	}
	
	public int size () {
		if ( vec != null ) return vec.size() ;
		else {
			try {
				Scanner scanner = new Scanner ( new File ( filename ) ) ;
				int result = scanner.nextInt() ;
				scanner.close() ; 
				return  result ; 
			} catch ( Exception e ) {
				e.printStackTrace() ;
			}
			return -1 ;
		}
	}
	
	public void save () throws Exception {
		PrintStream fout = new PrintStream ( new FileOutputStream ( filename )) ; 
		fout.println ( vec.size() ) ; 
		for ( Map.Entry<String,Integer> i : vec.entrySet() ) fout.println ( i.getKey() + "\t" + i.getValue() ) ;
		fout.close(); 
	}	
	
	public static String normallize ( String token ) {
		token = token.toLowerCase() ;
		token= token.replaceAll("[\\.\\\\/\\?'~\\^:]", "" ).trim() ;
		token = token.replace ( '眉' , 'u');
		token = token.replace ( '盲' , 'a');
		token = token.replace ( '枚' , 'o');
		token = token.replace ( "ue" , "u");
		token = token.replace ( "ae" , "a");
		token = token.replace ( "oe" , "o");
		token = token.replace ( '脽' , 's') ;			

		return token; 
	}
}
